package com.zznj.xzjc.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.vondear.rxtool.RxActivityTool;
import com.zznj.xzjc.ActivityAnJianDetail;
import com.zznj.xzjc.R;
import com.zznj.xzjc.adapter.AreaAdapter;
import com.zznj.xzjc.adapter.index.AjcxFsajAdapter;
import com.zznj.xzjc.adapter.index.ZzjgAdapter;
import com.zznj.xzjc.base.BaseActivity;
import com.zznj.xzjc.bean.index.AjcxData;
import com.zznj.xzjc.event.EventTag;
import com.zznj.xzjc.event.MessageEvent;
import com.zznj.xzjc.net.SharedHelper;
import com.zznj.xzjc.net.entity.AreaBean;
import com.zznj.xzjc.net.entity.BaseBean;
import com.zznj.xzjc.net.entity.LoginBean;
import com.zznj.xzjc.net.entity.ZzjgBean;
import com.zznj.xzjc.net.rxjava.HttpMethods;
import com.zznj.xzjc.net.subscribers.ProgressSubscriber;
import com.zznj.xzjc.net.subscribers.SubscriberOnNextListener;
import com.zznj.xzjc.utils.StringUtils;
import com.zznj.xzjc.view.MaxHeightRecyclerView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.FormBody;

/**
 * 描述：线索发现
 */
public class ActivityAjcxFsaj extends BaseActivity {
    Unbinder unbinder;

    @BindView(R.id.searchEdit)
    EditText searchEdit;
    @BindView(R.id.searchBtn)
    ImageView searchBtn;

    @BindView(R.id.xzqh_linear)
    View xzqh_linear;
    @BindView(R.id.xzqh_tv)
    TextView xzqh_tv;
    @BindView(R.id.xzqh_tv_xx)
    ImageView xzqh_tv_xx;


    @BindView(R.id.lrdw_linear)
    View lrdw_linear;
    @BindView(R.id.lrdw_tv)
    TextView lrdw_tv;
    @BindView(R.id.lrdw_tv_xx)
    ImageView lrdw_tv_xx;


    @BindView(R.id.srl_all)
    SmartRefreshLayout srlAll;
    @BindView(R.id.rec_main)
    RecyclerView rec_main;

    @BindView(R.id.bottomLinear)
    View bottomLinear;
    @BindView(R.id.tishiImg)
    ImageView tishiImg;


    @BindView(R.id.zheCeng)
    View zheCeng;
    @BindView(R.id.lrdwList)
    MaxHeightRecyclerView lrdwList;
    @BindView(R.id.xzqhList)
    MaxHeightRecyclerView xzqhList;

    @Override
    protected int getLayoutId() {
        return R.layout.gyss_activity_ajcx_fsaj_layout;
    }


    private LoginBean user;

    @Override
    public void initView() {
        user = new Gson().fromJson(SharedHelper.readValueByKeyForString(mContext, "Member"), LoginBean.class);
        setTitle("非诉案件");
        initSrl();
        init();
        initArea();
        initDw();
    }

    private List<AjcxData> list;
    private AjcxFsajAdapter mAdapter;

    private void init() {
        list = new ArrayList<>();
        ((SimpleItemAnimator) rec_main.getItemAnimator()).setSupportsChangeAnimations(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        rec_main.setLayoutManager(linearLayoutManager);
        mAdapter = new AjcxFsajAdapter(R.layout.gyss_item_fsaj, list);
        rec_main.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("caseId", list.get(position).getCaseId());
                RxActivityTool.skipActivity(mContext, ActivityAnJianDetail.class, bundle);
            }
        });
    }

    @Override
    protected void initData() {
        refresh();
        getArea();
        getZzjg();
    }

    private void initSrl() {
        srlAll.setEnableRefresh(true);
        srlAll.setEnableLoadMore(true);
        srlAll.setEnableAutoLoadMore(true);
        srlAll.setOnRefreshListener(refresh_layout -> {
            refresh();
        });
        srlAll.setOnLoadMoreListener(refresh_layout -> {
            isShowDialog = false;
            page++;
            getData();
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshData(MessageEvent messageEvent) {
        if (messageEvent.getTag().equals(EventTag.DT)) {
            refresh();
        }
    }


    private String districtCode = "";
    private String orgId = "";
    private AreaAdapter areaAdapter;
    private List<AreaBean> areaList;
    private int selectAreaPosition = -1;

    private void initArea() {
        areaList = new ArrayList<>();
        GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        xzqhList.setLayoutManager(layoutManager);
        areaAdapter = new AreaAdapter(R.layout.gyss_item_area, areaList);
        xzqhList.setAdapter(areaAdapter);
        areaAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                selectAreaPosition = position;
                areaAdapter.setSelectPosition(selectAreaPosition);

                districtCode = areaList.get(position).getDistrictCode();
                if(StringUtils.isEmpty(districtCode)){
                    lrdw_tv.setText("行政区划");
                }else{
                    xzqh_tv.setText(areaList.get(position).getDistrictName());
                }

                zheCeng.setVisibility(View.GONE);
                xzqhList.setVisibility(View.GONE);
                xzqh_tv.setTextColor(getResources().getColor(R.color.color_333333));
                xzqh_tv_xx.setImageResource(R.mipmap.gyss_xiala_img);

                selectImg = null;
                selectView = null;

                refresh();
            }
        });
    }

    private List<ZzjgBean> dwList;
    private ZzjgAdapter zzjgAdapter;
    private int selectZzjgPosition = -1;

    private void initDw() {
        dwList = new ArrayList<>();
        ((SimpleItemAnimator) lrdwList.getItemAnimator()).setSupportsChangeAnimations(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        lrdwList.setLayoutManager(linearLayoutManager);
        zzjgAdapter = new ZzjgAdapter(R.layout.gyss_item_select_dictionary, dwList);
        lrdwList.setAdapter(zzjgAdapter);
        zzjgAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                selectZzjgPosition = position;
                zzjgAdapter.setSelectPosition(selectZzjgPosition);
                orgId = dwList.get(position).getOrgCode() + "";
                if(StringUtils.isEmpty(orgId)){
                    lrdw_tv.setText("录入单位");
                }else{
                    lrdw_tv.setText(dwList.get(position).getModelName());
                }

                zheCeng.setVisibility(View.GONE);
                lrdwList.setVisibility(View.GONE);

                lrdw_tv.setTextColor(getResources().getColor(R.color.color_333333));
                lrdw_tv_xx.setImageResource(R.mipmap.gyss_xiala_img);

                selectImg = null;
                selectView = null;

                refresh();
            }
        });
    }

    private void getArea() {
        SubscriberOnNextListener<BaseBean<List<AreaBean>>> onNextListener = baseBean -> {
            areaList.addAll(baseBean.getResult());
            AreaBean bean = new AreaBean();
            bean.setDistrictCode("");
            bean.setDistrictName("行政区划(全部)");
            areaList.add(0,bean);
            areaAdapter.notifyDataSetChanged();
            areaAdapter.setSelectPosition(selectAreaPosition);
        };
        HttpMethods.getInstance().getDistrictByJb(new ProgressSubscriber<>(onNextListener, mContext, true));
    }

    private void getZzjg() {
        SubscriberOnNextListener<BaseBean<List<ZzjgBean>>> onNextListener = baseBean -> {
            dwList.addAll(baseBean.getResult());
            ZzjgBean jg = new ZzjgBean();
            jg.setOrgCode("");
            jg.setModelName("录入单位(全部)");
            dwList.add(0,jg);

            zzjgAdapter.notifyDataSetChanged();
            zzjgAdapter.setSelectPosition(selectZzjgPosition);
        };
        HttpMethods.getInstance().getOrgTreeData(new ProgressSubscriber<>(onNextListener, mContext, true));
    }


    private boolean isShowDialog = true;
    private int page = 0;

    public void refresh() {
        srlAll.setEnableLoadMore(true);
        isShowDialog = false;
        page = 1;
        srlAll.setEnableLoadMore(true);
        list.clear();
        bottomLinear.setVisibility(View.GONE);
        mAdapter.notifyDataSetChanged();
        getData();
    }

    private void getData() {
        SubscriberOnNextListener<BaseBean<List<AjcxData>>> onNextListener = baseBean -> {
            if (srlAll != null) {
                srlAll.finishLoadMore(true);
                srlAll.finishRefresh(true);
            }
            if (list.size() + baseBean.getResult().size() > 0 && baseBean.getResult().size() < 10) {
                bottomLinear.setVisibility(View.VISIBLE);
                if (srlAll != null) {
                    srlAll.setEnableLoadMore(false);
                }
            }
            list.addAll(baseBean.getResult());
            mAdapter.notifyDataSetChanged();


            if (mAdapter.getItemCount() == 0) {
                tishiImg.setVisibility(View.VISIBLE);
                srlAll.setVisibility(View.GONE);
                tishiImg.setImageResource(R.mipmap.gyss_zwnr_img);
            } else {
                tishiImg.setVisibility(View.GONE);
                srlAll.setVisibility(View.VISIBLE);
            }
        };


        FormBody.Builder builder = new FormBody.Builder()
                .add("condition", searchEdit.getText().toString())
                .add("page", page + "")
                .add("districtCode", districtCode)
                .add("orgId", orgId);

        HttpMethods.getInstance().feisuQuery(new ProgressSubscriber<>(onNextListener, mContext, true), builder.build());
    }

    private TextView selectView;
    private ImageView selectImg;

    @OnClick({R.id.xzqh_linear, R.id.lrdw_linear,R.id.searchBtn})
    public void onClick(View v) {
        if (v.getId() == xzqh_linear.getId()) {
            lrdwList.setVisibility(View.GONE);
            if (selectView != xzqh_tv) {
                zheCeng.setVisibility(View.VISIBLE);
                xzqhList.setVisibility(View.VISIBLE);
                xzqh_tv.setTextColor(getResources().getColor(R.color.title_bg));
                xzqh_tv_xx.setImageResource(R.mipmap.gyss_xiangxia_select);
                selectView = xzqh_tv;
                selectImg = xzqh_tv_xx;
            } else {
                zheCeng.setVisibility(View.GONE);
                xzqhList.setVisibility(View.GONE);
                xzqh_tv.setTextColor(getResources().getColor(R.color.color_333333));
                xzqh_tv_xx.setImageResource(R.mipmap.gyss_xiala_img);
                selectView = null;
                selectImg = null;
            }
        } else if (v.getId() == lrdw_linear.getId()) {
            xzqhList.setVisibility(View.GONE);
            if (selectView != lrdw_tv) {
                zheCeng.setVisibility(View.VISIBLE);
                lrdwList.setVisibility(View.VISIBLE);
                lrdw_tv.setTextColor(getResources().getColor(R.color.title_bg));
                lrdw_tv_xx.setImageResource(R.mipmap.gyss_xiangxia_select);

                selectView = lrdw_tv;
                selectImg = lrdw_tv_xx;
            } else {
                zheCeng.setVisibility(View.GONE);
                lrdwList.setVisibility(View.GONE);
                lrdw_tv.setTextColor(getResources().getColor(R.color.color_333333));
                lrdw_tv_xx.setImageResource(R.mipmap.gyss_xiala_img);

                selectView = null;
                selectImg = null;
            }
        } else if (v.getId() == R.id.searchBtn) {
            refresh();
        }
    }
}
